package com.xinsoft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.CusNewsDTO;
import com.xinsoft.entity.dto.CusNewsParam;
import com.xinsoft.entity.po.CusNews;
import com.xinsoft.entity.po.CusOaDocument;
import com.xinsoft.entity.po.SysFileHis;
import com.xinsoft.service.CusNewsService;
import com.xinsoft.mapper.CusNewsMapper;
import com.xinsoft.service.CusOaDocumentService;
import com.xinsoft.service.SysFileHisService;
import com.xinsoft.utils.TokenUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author hsm
* @description 针对表【cus_news(新闻公告)】的数据库操作Service实现
* @createDate 2023-09-27 14:28:56
*/
@Service
public class CusNewsServiceImpl extends ServiceImpl<CusNewsMapper, CusNews>
    implements CusNewsService{

    @Resource
    private CusOaDocumentService documentService;

    @Resource
    private SysFileHisService sysFileHisService;

    @Override
    public IPage<CusNewsDTO> findPageListByParam(CusNewsParam param) {
        //分页
        Page<CusNewsDTO> page = new Page<CusNewsDTO>(param.getCurrentPage(), param.getPageSize());
        IPage<CusNewsDTO> resultPage = this.baseMapper.findListByParam(page, param);
        //返回值
        return resultPage;
    }

    @Override
    public CusNews addCusNews(CusNewsDTO cusNews) {

        CusNews entity = new CusNews();
        BeanUtils.copyProperties(cusNews,entity);
        cusNews.setPublishStatus(0);
        cusNews.setPublishBy(null);
        entity.removeId();
        save(entity);

        //保存图片
        if(CollectionUtil.isNotEmpty(cusNews.getSysFileHisList())){
            List<CusOaDocument> documentList = new ArrayList<>();
            for (SysFileHis sysFileHis : cusNews.getSysFileHisList()) {
                CusOaDocument document = new CusOaDocument();
                document.setDataId(entity.getId());
                document.setDocumentId(sysFileHis.getId());
                document.setType(Const.DOCUMENT_OA_TYPE.CUS_NEWS);
                documentList.add(document);
            }
            // 保存文档关系
            documentService.saveBatch(documentList);
        }
        return entity;
    }

    @Override
    public CusNews editCusNews(CusNewsDTO cusNews) {
        // 已发布的不能修改
        CusNews entity = getById(cusNews.getId());
        if(Objects.equals(entity.getPublishStatus(),Integer.valueOf(1))){
            throw new BDException("操作失败，已发布不能修改！");
        }
        BeanUtils.copyProperties(cusNews,entity);
        updateById(entity);
        //删除图片
        QueryWrapper<CusOaDocument> documentQuery = new QueryWrapper<>();
        documentQuery.eq("data_id", entity.getId());
        documentQuery.eq("type", Const.DOCUMENT_OA_TYPE.CUS_NEWS);
        documentService.remove(documentQuery);
        //保存图片
        if(CollectionUtil.isNotEmpty(cusNews.getSysFileHisList())){
            List<CusOaDocument> documentList = new ArrayList<>();
            for (SysFileHis sysFileHis : cusNews.getSysFileHisList()) {

                CusOaDocument document = new CusOaDocument();
                document.setDataId(entity.getId());
                document.setDocumentId(sysFileHis.getId());
                document.setType(Const.DOCUMENT_OA_TYPE.CUS_NEWS);
                documentList.add(document);
            }
            // 保存文档关系
            documentService.saveBatch(documentList);
        }
        return entity;
    }

    @Override
    public void del(List<Integer> ids) {
        List<CusNews> newsList = list(new QueryWrapper<CusNews>().eq("publish_status",1).in("id",ids));
        if(CollectionUtils.isNotEmpty(newsList)){
            throw new BDException("删除失败，标题为："+newsList.get(0).getTitle()+"的新闻公告已发布，不能删除！");
        }
        removeByIds(ids);
    }

    @Override
    public CusNewsDTO getCusNews(Integer id) {

        CusNewsDTO cusNewsDTO = this.baseMapper.findInfoById(id);
        //照片列表
        QueryWrapper queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("document_id");
        queryWrapper2.eq("data_id",id);
        queryWrapper2.eq("type", Const.DOCUMENT_OA_TYPE.CUS_NEWS);
        List<CusOaDocument> documentList2 = documentService.list(queryWrapper2);
        // 图片列表
        if(CollectionUtil.isNotEmpty(documentList2)){
            List<Integer> fileIds = documentList2.stream()
                    .map(CusOaDocument::getDocumentId)
                    .collect(Collectors.toList());
            QueryWrapper queryFileWrapper = new QueryWrapper<>();
            queryFileWrapper.in("id",fileIds);
            List<SysFileHis> fileHisList = sysFileHisService.list(queryFileWrapper);
            cusNewsDTO.setSysFileHisList(fileHisList);
        }
        return cusNewsDTO;
    }

    @Override
    public void publish(List<Integer> ids) {
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("publish_status",1);
        updateWrapper.set("publish_by", TokenUtil.getUserId());
        updateWrapper.in("id",ids);
        update(updateWrapper);
    }

    @Override
    public void dePublish(List<Integer> ids) {
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("publish_status",0);
        updateWrapper.set("publish_by", null);
        updateWrapper.in("id",ids);
        update(updateWrapper);
    }
}